// -------- PIN SETUP -------- #define BUTTON_PIN D8 // button input (INPUT_PULLDOWN) #define LED_PIN D2 // yellow LED // -------- TIMING CONFIG -------- const unsigned long debounceDelay = 50; const unsigned long longPressTime = 1000; const unsigned long doublePressGap = 400; // -------- SYSTEM STATE -------- int totalAmount = 0; int goal = 100; bool milestone25 = false; bool milestone50 = false; bool milestone75 = false; bool milestone100 = false; // -------- BUTTON STATE -------- bool buttonState = LOW; bool lastButtonReading = LOW; unsigned long lastDebounceTime = 0; unsigned long pressStartTime = 0; unsigned long lastReleaseTime = 0; int pressCount = 0; // -------- SETUP -------- void setup() { Serial.begin(115200); pinMode(BUTTON_PIN, INPUT_PULLDOWN); pinMode(LED_PIN, OUTPUT); digitalWrite(LED_PIN, LOW); // LED OFF initially } // -------- LOOP -------- void loop() { int reading = digitalRead(BUTTON_PIN); // -------- DEBOUNCE -------- if (reading != lastButtonReading) { lastDebounceTime = millis(); } if ((millis() - lastDebounceTime) > debounceDelay) { ``` if (reading != buttonState) { buttonState = reading; // -------- BUTTON PRESSED -------- if (buttonState == HIGH) { pressStartTime = millis(); } // -------- BUTTON RELEASED -------- else { unsigned long pressDuration = millis() - pressStartTime; // -------- LONG PRESS -------- if (pressDuration >= longPressTime) { handleLongPress(); pressCount = 0; } else { pressCount++; lastReleaseTime = millis(); } } } ``` } // -------- SINGLE / DOUBLE -------- if (pressCount > 0 && (millis() - lastReleaseTime) > doublePressGap) { ``` if (pressCount == 1) { handleSinglePress(); } else if (pressCount == 2) { handleDoublePress(); } pressCount = 0; ``` } lastButtonReading = reading; } // -------- ACTIONS -------- // Single press → ₹1 void handleSinglePress() { addAmount(1); blinkLED(1); } // Double press → ₹2 void handleDoublePress() { addAmount(2); blinkLED(2); } // Long press → ₹5 void handleLongPress() { addAmount(5); // longer glow digitalWrite(LED_PIN, HIGH); delay(400); digitalWrite(LED_PIN, LOW); } // -------- CORE LOGIC -------- void addAmount(int value) { // prevent adding after goal already reached if (milestone100) return; totalAmount += value; // clamp to goal if (totalAmount > goal) { totalAmount = goal; } int percentage = (totalAmount * 100) / goal; Serial.print("Added: "); Serial.print(value); Serial.print(" | Total: "); Serial.print(totalAmount); Serial.print(" | "); Serial.print(percentage); Serial.println("%"); checkMilestones(percentage); } // -------- LED HELPER -------- void blinkLED(int times) { for (int i = 0; i < times; i++) { digitalWrite(LED_PIN, HIGH); delay(120); digitalWrite(LED_PIN, LOW); delay(120); } } // -------- RESET SYSTEM -------- void resetSystem() { totalAmount = 0; milestone25 = false; milestone50 = false; milestone75 = false; milestone100 = false; Serial.println("System Reset → Back to 0%"); } // -------- MILESTONE LOGIC -------- void checkMilestones(int percentage) { if (!milestone25 && percentage >= 25) { milestone25 = true; Serial.println("Milestone: 25% reached"); } if (!milestone50 && percentage >= 50) { milestone50 = true; Serial.println("Milestone: 50% reached"); } if (!milestone75 && percentage >= 75) { milestone75 = true; Serial.println("Milestone: 75% reached"); } if (!milestone100 && percentage >= 100) { milestone100 = true; ``` Serial.println("Milestone: Goal reached!"); // -------- CELEBRATION -------- for (int i = 0; i < 3; i++) { digitalWrite(LED_PIN, HIGH); delay(150); digitalWrite(LED_PIN, LOW); delay(150); } delay(500); // pause before reset resetSystem(); // restart cycle ``` } }